#include <QPainter>
#include <QUrl>
#include <QRect>

#include "coverdelegate.h"
#include <QModelIndex>


void CoverDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    if (index.column() == 1) {
        QStyledItemDelegate::paint(painter, option, QModelIndex());
        QImage cover = index.data().value<QImage>();
        QRect coverRect = cover.rect();
        int factor = coverRect.height();
        factor /= option.rect.height();
        int height = coverRect.height() / factor;
        int width = coverRect.width() / factor;
        QRect newSize(option.rect.x(), option.rect.y(), width, height);
        painter->drawImage(newSize, cover);
    }
    else
        QStyledItemDelegate::paint(painter, option, index);
}

QSize CoverDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index)
{
    if (index.column() == 1)
        return index.data().value<QImage>().size();
    else
        return QStyledItemDelegate::sizeHint(option, index);
}
